package com.lanou3g.com;

import com.lanou3g.com.controller.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/16;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    private Controller controller = new Controller();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        // 拿到用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String word = request.getParameter("word");

        // 验证
        boolean registerSuccess
                = controller.register(username, password, word);

        String msg = "";
        if (registerSuccess){
            msg = "Register Success";
        } else {
            msg = "Register Failed";
        }
        // 把消息放到 request 域中 发送给jsp
        request.setAttribute("msg",msg);
        // 请求转发
        request.getRequestDispatcher("/user/msg.jsp")
                .forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
